﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using ZLDC.Common.Extensions;
using ZLDC.Transaction.Model;
using ZLDC.Common.BaseUser;

namespace ZLDC.Estate.UserControl
{
    public partial class Autocomplete : System.Web.UI.UserControl
    {
        private bool _isScript = false;
        private SelectType _selectType = SelectType.User;

        private ZLDC.Transaction.Model.Model_S_Autocomplete AutocompleteValue = new ZLDC.Transaction.Model.Model_S_Autocomplete();

        /// <summary>
        /// 设置或获取是否自动加载JS
        /// </summary>
        public bool IsScript
        {
            get { return _isScript; }
            set { _isScript = value; }
        }

        /// <summary>
        /// Autocomplete类型  先只支持 选择用户和部门
        /// </summary>
        public SelectType selectType
        {
            get { return _selectType; }
            set { _selectType = value; }
        }

        private string _select = string.Empty;
        /// <summary>
        /// 在选中时触发事件
        /// </summary>
        public string ClientChange
        {
            get { return _select; }
            set { _select = value; }
        }

        /// <summary>
        /// 
        /// </summary>
        public string Text
        {
            get { return TextBox1.Text; }
            set { TextBox1.Text = value; }
        }

        /// <summary>
        /// 设置或获取选择项的值
        /// </summary>
        public int SelectValue
        {
            get { return AutocompleteValue.Id; }
            set
            {
                ZLDC.Transaction.Model.Model_S_Alldept model;
                ZLDC.Transaction.BLL.BLL_S_Alldept bll2 = new ZLDC.Transaction.BLL.BLL_S_Alldept();
                switch (_selectType)
                {
                    case SelectType.Dept:
                        model = bll2.GetModel(new SqlDbQueryFactor("id", value));
                        AutocompleteValue.Id = model.Id;
                        AutocompleteValue.Name = model.Name;
                        AutocompleteValue.ShowName = model.Name;
                        TextBox1.Text = model.Name;
                        break;
                    case SelectType.User:
                        ZLDC.Transaction.BLL.BLL_S_Allusr bll = new ZLDC.Transaction.BLL.BLL_S_Allusr();
                        ZLDC.Transaction.Model.Model_S_Allusr model2 = bll.GetModel(new SqlDbQueryFactor("id", value));
                        model = bll2.GetModel(new SqlDbQueryFactor("id", model2.T_id));
                        AutocompleteValue.Id = model2.Id;
                        AutocompleteValue.Name = model2.Name;
                        AutocompleteValue.ShowName = model2.Name + "[" + model2.No + "," + model.Name + "]";
                        TextBox1.Text = AutocompleteValue.ShowName;
                        AutocompleteValue.Property1 = model.Id.ToString();
                        AutocompleteValue.Property2 = model.Name;
                        break;
                    case SelectType.Estate:
                        if (value == -1)
                        {
                            TextBox1.Text = "";
                            AutocompleteValue.Id = -1;
                            AutocompleteValue.Name = "";
                        }
                        break;
                }
                System.Web.Script.Serialization.JavaScriptSerializer Serializer = new System.Web.Script.Serialization.JavaScriptSerializer(new System.Web.Script.Serialization.SimpleTypeResolver());
                AutoInfo.Value = Serializer.Serialize(AutocompleteValue);
                //AutocompleteValue 
            }
        }

        /// <summary>
        /// 获得选中项文本
        /// </summary>
        public string SelectText
        {
            get { return AutocompleteValue.Name; }

        }

        public Model_S_Autocomplete SelectItem
        {
            get { return AutocompleteValue; }
        }

        public string AjaxMethod()
        {
            return "Get" + _selectType.ToString();
            //switch (_selectType)
            //{
            //    case SelectType.Dept:
            //        return "GetDept";
            //    case SelectType.User:
            //        return "GetUser";
            //    case SelectType.Estate:
            //        return "GetEstate";
            //    default:
            //        return "";
            //}
        }

        protected string _type = string.Empty;
        /// <summary>
        /// 
        /// </summary>
        public string Type
        {
            set { _type = value; }
            get { return _type; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string AjaxType()
        {
            if (Type == "")
            {
                return "";
            }
            return Type + "," + GetParam.m_sFgs;
        }
        /// <summary>
        /// 
        /// </summary>
        protected Param GetParam
        {
            get
            {
                return (Param)HttpContext.Current.Session["info"];
            }
        }
        /// <summary>
        /// 设置控件是否可用
        /// </summary>
        public bool Enabled
        {
            get { return TextBox1.Enabled; }
            set { TextBox1.Enabled = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (_isScript)
            {
                Literal script = new Literal();
                script.Text = " <script type=\"text/javascript\" src=\"../js/jquery-1.4.4.min.js\"></script>" +
                              " <script type=\"text/javascript\" src=\"../js/common.js\"></script>" +
                              " <script type=\"text/javascript\" src=\"../js/jquery-ui-1.8.7.custom.min.js\"></script>" +
                              " <link type=\"text/css\" href=\"../css/redmond/jquery-ui-1.8.8.custom.css\" rel=\"stylesheet\" />";
                //this.Page.Header.Controls.Add(ltHeader);
                this.Page.Header.Controls.Add(script);
            }

            if (AutoInfo.Value.Trim() != "")
            {
                string k = AutoInfo.Value;
                System.Web.Script.Serialization.JavaScriptSerializer Serializer = new System.Web.Script.Serialization.JavaScriptSerializer(new System.Web.Script.Serialization.SimpleTypeResolver());
                AutocompleteValue = Serializer.Deserialize<Model_S_Autocomplete>(k);
                //AutocompleteValue 
            }
            if (ClientChange.Trim() != "")
            {
                TextBox1.Attributes.Add("onmouseout", ClientChange);
            }
        }

        /// <summary>
        /// 设置客户端样式
        /// </summary>
        public string Style
        {
            set { TextBox1.Style.Value = value; }
        }


    }

    /// <summary>
    /// Autocomplete 初始化类型
    /// </summary>
    public enum SelectType
    {
        /// <summary>
        /// 部门选择
        /// </summary>
        Dept,
        /// <summary>
        /// 用户选择
        /// </summary>
        User,
        /// <summary>
        /// 楼盘
        /// </summary>
        Estate,
        /// <summary>
        /// 铺位
        /// </summary>
        Pos
    }

}